package com.barunlab.ems.server.push;

import org.apache.log4j.Logger;

import com.barunlab.ems.library.QueueWorker;
import com.barunlab.ems.model.EmsEvent;
import com.barunlab.ems.rmi.ClientInterface;
import com.barunlab.ems.server.main.ServerProperties;

/**
 * 클라이언트 세션을 개별 관리하는 클래스
 * 
 * @author 배수현
 * @version SKT SI R1.0
 * @since JDK6.0
 * @created 2013
 */
public class ClientSession extends QueueWorker<EmsEvent> {
	private static Logger LOGGER = Logger.getLogger(ClientSession.class);
	
	private String userName; 
	private ClientInterface clientInterface;
	
	private int failCount;
	
	public ClientSession(String userName, ClientInterface clientInterface) {
		this.userName = userName;
		this.clientInterface = clientInterface;
	}
	
	/**
	 * 클라이언트 세션에 해당하는 운용자명
	 * @return 운용자명
	 */
	public String getUserName() {
		return userName;
	}
	
	@Override
	public void processObject(EmsEvent event) {
		try {
			clientInterface.push(event);
			
			failCount = 0;
		}
		catch (Exception ex) {
			LOGGER.error(ex.getMessage(), ex);
			
			failCount++;
		}
		
		if ( failCount >= ServerProperties.CLIENT_SEND_FAIL_COUNT ) {
			LOGGER.error(String.format("클라이언트 전송 실패 횟수(%s)가 임계치를 초과했습니다. 해당 클라이언트를 제외합니다.", failCount));
			
			//UserInfoManager.logout(userName);
			ClientSessionManager.unregisterClient(userName);
		}
	}
}
